package utils;
/**
 * Project Name: MidTermQuiz
 * File Name: WordUtil.java
 */

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**Function: Remove Stopwords
 * 
 * @author Yicong Liang
 *
 */

public class WordUtil {

	private List<String> stopWordList = new ArrayList<String>();

	public WordUtil() {
		try {
			retrieveStopWords();
		} catch (Exception ex) {
			System.out.println("Error: Could not get stopword List!");
		}
	}

	/*
	 * Retrieve stopwords list from URL
	 */
	private void retrieveStopWords() throws IOException {
		String path = "D:\\CityU\\book recommendation\\common-english-words.txt";
		File f = new File(path);
		BufferedReader reader = new BufferedReader(new FileReader(f));
		try {
			String line = null;
			while ((line = reader.readLine()) != null) {
				String[] words = line.split(",");
				Collections.addAll(stopWordList, words);
			}
		} catch (IOException ex) {
			System.out.println("It may be a wrong file path");
		} finally {
			if (reader != null)
				reader.close();
		}
	}

	/**
	 * This function judges whether a word is stopword. From
	 * common-english-words.txt
	 */
	public boolean isStopWord(String word) {
		word = word.toLowerCase();
		if (stopWordList != null && stopWordList.contains(word)) {
			return true;
		}
		return false;
	}
	
	public boolean isNumber(String word) {
		Pattern pattern = Pattern.compile("(\\d).*");
		Matcher isNum = pattern.matcher(word);
		if (isNum.matches()) {
			//System.out.println("===found num "+word+" =====");
			return true;
		}
		
		return false;
	}
	
	public String removeNoise(String str){
		String result=str;
		int length=str.length();
		if(str.endsWith(".")||str.endsWith("%")||str.endsWith("$")){
			result=str.substring(0,length-1);
		}
		if(str.endsWith("'s")){
			result=str.substring(0,length-2);
		}
				
		return result;
	}
	
	public boolean isNoise(String word){
		boolean flag=false;
		if(word.indexOf("$")!=-1){
			flag = true;
		}
		
		return flag;
	}
	

	public List<String> getStopWordList() {
		return stopWordList;
	}

}
